$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/user/list',
        datatype: "json",
        colModel: [
            { label: '用户名', name: 'username', width: 40 },
            { label: '用户id', name: 'userId', width: 20, key: true },
            { label: '邮箱', name: 'email', width: 40 },
            { label: '手机号', name: 'mobile', width: 40 },
			{ label: '状态', name: 'status', width: 40 },
			{ label: '创建时间', name: 'createTime', width: 40 },
        ],
		viewrecords: true,
        height: 720,
        rowNum: 19,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

   

});

var vm = new Vue({
	el:'#dcapp',
	data:{
		q:{
			name: null,
		},
		showList: true,
		title: null,
        store: {}
	},
    created: function(){
        
    },
	methods: {
		query: function () {
			vm.reload();
		},
		update: function () {
		    var storeId = getSelectedRow();//选择一条
            if(storeId == null){
                return ;
            }
            vm.getStore(storeId);
			vm.showList = false;
			vm.title = "编辑门店";
        },
        add: function(){
        	vm.store={};
			vm.showList = false;
			vm.title = "增加门店";
		},
		saveOrUpdate: function () {
            var url = vm.store.id == null ? "store/saveStore" : "store/updateStore";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.store),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        del: function () {
            var storeIds = getSelectedRows();
            if(storeIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "store/delete",
                    contentType: "application/json",
                    data: JSON.stringify(storeIds),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
    	getStore: function(storeId){
			$.get(baseURL + "store/info/"+storeId, function(r){
				vm.store = r.store;
			});
		},
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.username},
                page:page
            }).trigger("reloadGrid");
		}
	}
});